﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XMK.Utils;
using XMK.Tadong.Model;
using System.Data;
using XMK.Tadong.Biz;

namespace XMK.Tadong.Web.SpaceClassView
{
    /// <summary>
    /// DetailHandler 的摘要说明
    /// </summary>
    public class DetailHandler : IHttpHandler
    {
        private string personId = "48f3889c-af8d-401f-ada2-c383031af92d1";
        private string teamId = "16";
        public void ProcessRequest(HttpContext context)
        {

            string currrentForldId = context.Request.Params.Get("forldId");//当前文件夹Id
            string currentClassType = context.Request.Params.Get("classType");//当前分类类别
            string spaceType = context.Request.Params.Get("spaceType");//空间类别10-个人空间，20-课题组空间，30-项目库空间
            string groupItem = context.Request.Params.Get("groupItem");//分类条件
            teamId = context.Request.Params.Get("teamId");//课题组id
            personId = context.Request.Params.Get("userId");
            string resultJson = "";
            switch (currentClassType)
            {
                case "CREATE_TIME": { resultJson = getDataByTime(currrentForldId, spaceType, currentClassType, groupItem); break; }
                case "FILE_SIZE": { resultJson = getDataByByte(currrentForldId, spaceType, currentClassType, groupItem); break; }
                case "DEST_OBJ": { resultJson = getDataByObj(currrentForldId, spaceType, currentClassType, groupItem); break; }
                case "BIZ_OBJ": { resultJson = getDataByBiz(currrentForldId, spaceType, currentClassType, groupItem); break; }
                case "FILE_EXT": { resultJson = getDataByType(currrentForldId, spaceType, currentClassType, groupItem); break; }

            }
            context.Response.Write(resultJson);

        }
         //根据时间分类
        public string getDataByTime(string currrentForldId, string spaceType, string classType, string groupItem)   
        {
            DataTable dt = null;
            Tb_SpaceBiz biz = new Tb_SpaceBiz();
            string sqlwhere;
            if (groupItem == "week")
            {//本周
                sqlwhere = "(trunc(sysdate)-trunc(create_time)>0 and  trunc(sysdate)-trunc(create_time)<7) and folder_id=" + currrentForldId + "";                 
            }
            else if (groupItem == DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString())
            { //本月
                sqlwhere = " (create_time >=TRUNC(SYSDATE, 'MM') and create_time<=last_day(SYSDATE)) and folder_id=" + currrentForldId + "";
            }
            else if (groupItem == (DateTime.Now.Year - 1).ToString())
            { //去年
                sqlwhere = "((create_time>=add_months(trunc(sysdate, 'yyyy'), -12)))  and  create_time<=add_months(trunc(sysdate, 'yyyy'), 0)-1 and folder_id=" + currrentForldId + ""; 
            }
            else if (groupItem == "early")
            { //更早
                sqlwhere = "(create_time<add_months(trunc(sysdate, 'yyyy'), -12))  and folder_id=" + currrentForldId + "";    
            }
            else
            { //本年前几个月
                sqlwhere = " (create_time<last_day(to_date('" + groupItem + "','yyyy/mm'))+1 and create_time>trunc(to_date('" + groupItem + "','yyyy/mm'))) and folder_id=" + currrentForldId + "";    
            }

            dt = biz.getXmkClassDetailFile(personId,teamId, sqlwhere, spaceType);
            
            return JsonHelper.ToJson(dt);
        }
        //根据大小分类
        public string getDataByByte(string currrentForldId, string spaceType, string classType, string groupItem)
        {
            DataTable dt = null;
            string sqlwhere = "";
            Tb_SpaceBiz biz = new Tb_SpaceBiz();
            if (groupItem == "1k-1m")
            {
                sqlwhere = "(FILE_SIZE LIKE '%K%' OR FILE_SIZE LIKE'%byte%' OR FILE_SIZE LIKE'%k%') and folder_id=" + currrentForldId + "";
                dt = biz.getXmkClassDetailFile(personId, teamId, sqlwhere, spaceType);
                return JsonHelper.ToJson(dt);
            }
            else
            {
                sqlwhere = "(FILE_SIZE LIKE '%m%' OR FILE_SIZE LIKE'%M%' OR FILE_SIZE LIKE'%mb%' OR FILE_SIZE LIKE'%MB%') and folder_id=" + currrentForldId + "";
                dt = biz.getXmkClassDetailFile(personId, teamId, sqlwhere, spaceType); ;
                if (groupItem == "1m-2m")
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string file_size = dt.Rows[i]["FILE_SIZE"].ToString().Trim().ToLower();
                        file_size = file_size.Replace("mb", "");
                        file_size = file_size.Replace('m', ' ');
                        if ((Convert.ToInt32(file_size) > 1 || Convert.ToInt32(file_size) == 1) && (Convert.ToInt32(file_size) < 2))
                        {
                            continue;
                        }
                        else 
                        {
                            dt.Rows.RemoveAt(i);
                            i--;
                        }
                            
                    }
                }
                else if(groupItem == "2m-3m")
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string file_size = dt.Rows[i]["FILE_SIZE"].ToString().Trim().ToLower();
                        file_size = file_size.Replace("mb", "");
                        file_size = file_size.Replace('m', ' ');
                        if((Convert.ToInt32(file_size) > 2 || Convert.ToInt32(file_size) == 2) && (Convert.ToInt32(file_size) < 3))
                        {
                            continue;
                        }
                        else
                        {
                            dt.Rows.RemoveAt(i);
                            i--;
                        }

                    }
                }
                else if (groupItem == "3m-4m")
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string file_size = dt.Rows[i]["FILE_SIZE"].ToString().Trim().ToLower();
                        file_size = file_size.Replace("mb", "");
                        file_size = file_size.Replace('m', ' ');
                        if ((Convert.ToInt32(file_size) > 3 || Convert.ToInt32(file_size) == 3) && (Convert.ToInt32(file_size) < 4))
                        {
                            continue;
                        }
                        else
                        {
                            dt.Rows.RemoveAt(i);
                            i--;
                        }

                    }
                }
                else if (groupItem == "4m-5m")
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string file_size = dt.Rows[i]["FILE_SIZE"].ToString().Trim().ToLower();
                        file_size = file_size.Replace("mb", "");
                        file_size = file_size.Replace('m', ' ');
                        if ((Convert.ToInt32(file_size) > 4 || Convert.ToInt32(file_size) == 4) && (Convert.ToInt32(file_size) < 5))
                        {
                            continue;
                        }
                        else
                        {
                            dt.Rows.RemoveAt(i);
                            i--;
                        }

                    }
                }
                else if (groupItem == "5m-10m")
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string file_size = dt.Rows[i]["FILE_SIZE"].ToString().Trim().ToLower();
                        file_size = file_size.Replace("mb", "");
                        file_size = file_size.Replace('m', ' ');
                        if ((Convert.ToInt32(file_size) > 5 || Convert.ToInt32(file_size) == 5) && (Convert.ToInt32(file_size) < 10))
                        {
                            continue;
                        }
                        else
                        {
                            dt.Rows.RemoveAt(i);
                            i--;
                        }

                    }
                }
                else if (groupItem == "10m-20m")
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string file_size = dt.Rows[i]["FILE_SIZE"].ToString().Trim().ToLower();
                        file_size = file_size.Replace("mb", "");
                        file_size = file_size.Replace('m', ' ');
                        if ((Convert.ToInt32(file_size) > 10 || Convert.ToInt32(file_size) == 10) && (Convert.ToInt32(file_size) < 20))
                        {
                            continue;
                        }
                        else
                        {
                            dt.Rows.RemoveAt(i);
                            i--;
                        }

                    }
                }
                else if (groupItem == "20m-50m")
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string file_size = dt.Rows[i]["FILE_SIZE"].ToString().Trim().ToLower();
                        file_size = file_size.Replace("mb", "");
                        file_size = file_size.Replace('m', ' ');
                        if ((Convert.ToInt32(file_size) > 20 || Convert.ToInt32(file_size) == 20) && (Convert.ToInt32(file_size) < 50))
                        {
                            continue;
                        }
                        else
                        {
                            dt.Rows.RemoveAt(i);
                            i--;
                        }

                    }
                }
                else if (groupItem == "50m-*")
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string file_size = dt.Rows[i]["FILE_SIZE"].ToString().Trim().ToLower();
                        file_size = file_size.Replace("mb", "");
                        file_size = file_size.Replace('m', ' ');
                        if (Convert.ToInt32(file_size) > 50 || Convert.ToInt32(file_size) == 50)
                        {
                            continue;
                        }
                        else
                        {
                            dt.Rows.RemoveAt(i);
                            i--;
                        }

                    }
                }
                
               
            }
            return JsonHelper.ToJson(dt);
        }
        //根据目标分类
        public string getDataByObj(string currrentForldId, string spaceType, string classType, string groupItem)
        {
            DataTable dt = null;
            string sqlwhere;
            
            if (groupItem == "other")
            {
                sqlwhere = "(DEST_OBJ is null) and folder_id=" + currrentForldId + "";
            }
            else
            {
                sqlwhere = "(instr(DEST_OBJ, '" + groupItem + "') > 0) and folder_id=" + currrentForldId + "";
            }
            Tb_SpaceBiz biz = new Tb_SpaceBiz();
            dt = biz.getXmkClassDetailFile(personId, teamId, sqlwhere, spaceType);
           

            return JsonHelper.ToJson(dt);
        }
        //根据业务分类
        public string getDataByBiz(string currrentForldId, string spaceType, string classType, string groupItem)
        {
            DataTable dt = null;
            string sqlwhere;
            if (groupItem == "other")
            {
                sqlwhere = "(BIZ_OBJ is null) and folder_id=" + currrentForldId + "";
            }
            else
            {
                sqlwhere = "(instr(BIZ_OBJ, '" + groupItem + "') > 0 )and folder_id=" + currrentForldId + "";
            }
            Tb_SpaceBiz biz = new Tb_SpaceBiz();
            dt = biz.getXmkClassDetailFile(personId, teamId, sqlwhere, spaceType);
          
            
            return JsonHelper.ToJson(dt);
        }
        //根据格式分类
        public string getDataByType(string currrentForldId, string spaceType, string classType, string groupItem)
        {
            DataTable dt = null;

            EXTBiz extbiz = new EXTBiz();
            DataTable dt_ext = extbiz.QueryExtList();
            bool conf = false;//标示当前格式是否在定义格式中
            for (int i = 0; i < dt_ext.Rows.Count; i++)
            {
                if (groupItem == dt_ext.Rows[i][0].ToString())
                {
                    conf = true;
                    break;
                }
            }

            string sqlwhere = "";
            if (conf)
            {
                sqlwhere = "file_ext in (select item_code from ext_item inner join ext_type on ext_item.ext_id=ext_type.ext_id where ext_type.ext_code='" + groupItem + "' and folder_id=" + currrentForldId + " )";
            }
            else
            {
                sqlwhere = "file_ext='" + groupItem + "' and folder_id=" + currrentForldId + " ";
            }
            Tb_SpaceBiz biz = new Tb_SpaceBiz();
            dt = biz.getXmkClassDetailFile(personId, teamId, sqlwhere, spaceType);
            
        
            return JsonHelper.ToJson(dt);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}